package com.cainli.szbus.data.factory;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cainli.szbus.config.JSONTag;
import com.cainli.szbus.data.model.BusLine;
import com.cainli.szbus.log.CLog;
import com.foxykeep.datadroid.exception.DataException;
/**
 * 
 * @author cainli
 *
 */
public class BusLineListJsonFactory {

	private static final String TAG = BusLineListJsonFactory.class
			.getSimpleName();

	private BusLineListJsonFactory() {
		// No public constructor
	}

	public static ArrayList<BusLine> parseResult(String wsResponse)
			throws DataException {
		ArrayList<BusLine> busLineList = new ArrayList<BusLine>();
		try {
			JSONObject parser = new JSONObject(wsResponse);
			JSONObject jsonRoot = parser
					.getJSONObject(JSONTag.BUSLINE_LIST_ELEM_LINES);
			JSONArray jsonPersonArray = jsonRoot
					.getJSONArray(JSONTag.BUSLINE_LIST_ELEM_LINE);
			int size = jsonPersonArray.length();
			for (int i = 0; i < size; i++) {
				JSONObject jsonPerson = jsonPersonArray.getJSONObject(i);
				BusLine busline = new BusLine();
				busline.name = jsonPerson
						.getString(JSONTag.BUSLINE_LIST_ELEM_LINE_NAME);
				busline.info = jsonPerson
						.getString(JSONTag.BUSLINE_LIST_ELEM_LINE_INFO);
				busline.stats = jsonPerson
						.getString(JSONTag.BUSLINE_LIST_ELEM_LINE_STATS);
				busline.stat_xys = jsonPerson
						.getString(JSONTag.BUSLINE_LIST_ELEM_LINE_STAT_XYS);
				busline.xys = jsonPerson
						.getString(JSONTag.BUSLINE_LIST_ELEM_LINE_XYS);
				busLineList.add(busline);
			}
		} catch (JSONException e) {
			CLog.e(TAG, "JSONException", e);
			throw new DataException(e);
		}

		return busLineList;
	}
}
